<?Lassoscript
// Last modified 12/14/11 by ECL, Landmann InterActive

// FUNCTIONALITY
// Include for Slider Group add response page

// CHANGE NOTES
// 8/7/11
// First implementation.
// 12/14/11
// Adding Slider Config feature

// Debugging
// Var('svDebug' = 'Y');

// Converting action_params
Var('vError' = '');
Var('vOption' = '');
// The action_param "Action" is passed by the frm_slidergroup.inc form to indicate whether it is an add or edit
// Action = Update, means the request was an Edit (from frm_slidergroup.inc)
// Action = Add, means the request was an Add (from frm_slidergroup.inc)
Var('vAction' = Action_Param('Action'));

If($svDebug == 'Y');
	'<br>\n';
	'<b>slidergroup_addresponse.inc</b><br>\n';
/If;

// Checking referrer to make sure it's only posting from the Add page
// ============= ADD ========
If($vAction == 'Add');

	// Convert action_params
	Include(($svAdminHelpersPath)+'slidergroup_params.inc');

	// If an error was generated from the Params, no point continuing, so bail immediately
	If($vError == '');

		// Construct the query to add the record
		Var('SQLQueryAddSliderGroup' = (StripBackticks('/* SQLQueryAddSliderGroup */
INSERT INTO '+($svSiteDatabase)+'.'+($svSliderGroupsTable)+
	' SET
		Slider_TypeID="'+($SliderTypeIDSubmit)+'",
		sg_name="'+(Encode_SQL($vsg_name))+'",
		Active="'+(Encode_SQL($vActive))+'",
		DateCreated="'+(Date_Format(Date_GetCurrentDate,-DateFormat='%Q'))+'",
		DateModified="'+(Date_Format(Date_GetCurrentDate,-DateFormat='%Q'))+'";
SELECT LAST_INSERT_ID() AS NewSliderGroupID;')));
			
		If($svDebug == 'Y');
			('51: SQLQueryAddSliderGroup = '+($SQLQueryAddSliderGroup)+'<br>\n');
		/If;
	
		Inline($IV_Sliders, -Table=$svSliderGroupsTable, -SQL=$SQLQueryAddSliderGroup);
			
			If((Error_CurrentError) == (Error_NoError));
		
				// If no error, Add was successful, dump out Error 1001 "Add Successful"
				// Copying NewSliderGroupID to vNewID to use in links
				$vError = '1001';
				$vOption = Var('vsg_name');
				Var('vNewID' = Field('NewSliderGroupID'));
				If($svDebug == 'Y');
					('64: vError = '+($vError)+'<br>\n');
				/If;
		
			// There was an error
			Else;
				$vError = (Error_CurrentError);
			/If;

		/Inline;

		// Sliderconfigs Records
		// No need to delete the old SliderConfigs Record as we do on update - it's a new record!
		Var('SQLUpdateSliderConfig' = ('/* SQLUpdateSliderConfig */\n'));

		// $SliderConfigParamsArray is defined in slidergroup_params.inc
		Iterate($SliderConfigParamsArray, Local('i'));
			$SQLUpdateSliderConfig += ('INSERT INTO '+($svSliderConfigTable)+' SET\n');
			$SQLUpdateSliderConfig += (StripBackticks('SG_GroupID="'+($vNewID)'", '));
			$SQLUpdateSliderConfig += (StripBackticks('Slider_SliderTypeID="'+($SliderTypeIDSubmit)'",\n'));
			$SQLUpdateSliderConfig += (StripBackticks('\tSliderConfigName="'+(#i->get('1'))+'", SliderConfigValue="'+(#i->get('2'))+'";\n'));
		/Iterate;
		$SQLUpdateSliderConfig->RemoveTrailing(',\n');

		If($svDebug == 'Y');
			('88: SQLUpdateSliderConfig = '+($SQLUpdateSliderConfig)+'<br>\n');
		/If;
	
		Inline($IV_Sliders, -Table=$svSliderConfigTable, -SQL=$SQLUpdateSliderConfig);

			If((Error_CurrentError) == (Error_NoError));
				// If no error, Add was successful, dump out Error 1001 "Add Successful"
				$vError = '1001';
				$vOption = Var('vID');
				If($svDebug == 'Y');
					('144: vError = '+($vError)+'<br>\n');
				/If;
			// There was an error
			Else;
				$vError = (Error_CurrentError);
			/If;

		/Inline;

	/If;

// ============= EDIT ========
// Action = Update, means the request came from the Edit page
Else($vAction == 'Update');

	// Convert action_params
	Include(($svAdminHelpersPath)+'slidergroup_params.inc');
	Var('vID' = Action_Param('id'));

	// Copying the ID to vNewID to build link
	// This is so we can just use vNewID at bottom of page for both add and edit
	Var('vNewID' = Var('vID'));

	// If an error was generated from the Params, no point continuing, so bail immediately
	If($vError == '');

		// Construct the query to Update the record
		// Not changing the Date_Created fields
		Var('SQLUpdateSliderGroup' = (StripBackticks('/* SQLUpdateSliderGroup */
UPDATE '+($svSliderGroupsTable)+' SET
	sg_name="'+(Encode_SQL($vsg_name))+'",
	Active="'+(Encode_SQL($vActive))+'",
	DateModified="'+(Date_Format(Date_GetCurrentDate,-DateFormat='%Q'))+'"
WHERE SG_GroupID = "'+($vID)+'"')));

		If($svDebug == 'Y');
			('134: SQLUpdateSliderGroup = '+($SQLUpdateSliderGroup)+'<br>\n');
		/If;
	
		Inline($IV_Sliders, -Table=$svSliderGroupsTable, -SQL=$SQLUpdateSliderGroup);

			If((Error_CurrentError) == (Error_NoError));
				// If no error, Update was successful, dump out Error 1011 "Update Successful"
				$vError = '1011';
				$vOption = Var('vID');
				If($svDebug == 'Y');
					('144: vError = '+($vError)+'<br>\n');
				/If;
			// There was an error
			Else;
				$vError = (Error_CurrentError);
			/If;

		/Inline;

		// Sliderconfigs Records
		// DELETE the old SliderConfigs Record and ADD the new SliderConfigs
		Var('SQLUpdateSliderConfig' = (StripBackticks('/* SQLUpdateSliderConfig */
DELETE FROM '+($svSliderConfigTable)+' WHERE SG_GroupID="'+($vID)+'"
AND Slider_SliderTypeID="'+($SliderTypeIDSubmit)+'";\n')));

		// $SliderConfigParamsArray is defined in slidergroup_params.inc
		Iterate($SliderConfigParamsArray, Local('i'));
			$SQLUpdateSliderConfig += ('INSERT INTO '+($svSliderConfigTable)+' SET\n');
			$SQLUpdateSliderConfig += ('SG_GroupID="'+($vID)'", ');
			$SQLUpdateSliderConfig += ('Slider_SliderTypeID="'+($SliderTypeIDSubmit)'",\n');
			$SQLUpdateSliderConfig += ('\tSliderConfigName="'+(#i->get('1'))+'", SliderConfigValue="'+(#i->get('2'))+'";\n');
		/Iterate;
		$SQLUpdateSliderConfig->RemoveTrailing(',\n');

		If($svDebug == 'Y');
			('169: SQLUpdateSliderConfig = '+($SQLUpdateSliderConfig)+'<br>\n');
		/If;
	
		Inline($IV_Sliders, -Table=$svSliderConfigTable, -SQL=$SQLUpdateSliderConfig);

			If((Error_CurrentError) == (Error_NoError));
				// If no error, Update was successful, dump out Error 1011 "Update Successful"
				$vError = '1011';
				$vOption = Var('vID');
				If($svDebug == 'Y');
					('179: vError = '+($vError)+'<br>\n');
				/If;
			// There was an error
			Else;
				$vError = (Error_CurrentError);
			/If;

		/Inline;

	// vError == ''
	/If;

// ============= ASSIGN ========
Else($vAction == 'Assign');
	// Convert action_params
	Include(($svAdminHelpersPath)+'slidergroupassign_params.inc');

	// If an error was generated from the Params, no point continuing, so bail immediately
	If($vError == '');

		// First delete all current assignments, then add the new ones
		Var('SQLDeleteSG2P' = (StripBackticks('DELETE FROM '+($svSG2SliderTable)+' WHERE SG_GroupID = "'+(Var('vSliderGroupID'))+'";')));
		Inline($IV_Sliders, -SQL=$SQLDeleteSG2P);
		/Inline;
		
		// Construct the query to Update the Project
		Var('SliderEntryArray' = array);

		// Append an extra return to fix problem of only one item being submit
		Var('SliderEntryArray' = (Var('vSlider_Entries')->split('\r')));
		If($svDebug == 'Y');
			('271: SliderEntryArray = '+($SliderEntryArray)+'<br>\n');
		/If;

		// Initialize the query string
		Var('SQLAddSG2P' = string);

		Iterate($SliderEntryArray, Local('i'));

			// Copy the current iterate data to a local var for insertion into the query
			Local('ThisArrayElement'= #i);
			
			// Iterate through the Groups array, adding each INSERT command to the end
			$SQLAddSG2P += ('INSERT INTO '+($svSG2SliderTable)+'(
				SG_GroupID,
				slider_id)
				VALUES
				("'+(Var('vSliderGroupID'))+'",
				"'+(#ThisArrayElement)+'");');
			If($svDebug == 'Y');
				('285: ThisArrayElement = '+(#ThisArrayElement)+'<br>\n');
				('285: SQLAddSG2P = '+($SQLAddSG2P)+'<br>\n');
			/If;

		/Iterate;

		// Add the record
		Inline($IV_Sliders, -SQL=$SQLAddSG2P);
		/Inline;

		If((Error_CurrentError) == (Error_NoError));
	
			// If no error, Update was successful, dump out Error 1011 "Update Successful"
			$vError = '1011';
			$vOption = 'slider group assignment';
	
		// There was an error
		Else;
			$vError = Error_CurrentError;
		/If;
		

	// vError == ''
	/If;

	If($svDebug == 'Y');
		('311: vError = '+($vError)+'<br>\n');
		('311: vOption = '+($vOption)+'<br>\n');
	/If;

// ============= DELETE ========
Else($vAction == 'Delete');

	// Convert action_params
	Var('vID' = Action_Param('ID'));
	Var('SliderTypeID' = Action_Param('SliderTypeID'));

	// Build the Delete Query
	Var('SQLDeleteQuery' = string);
	Var('SQLDeleteQuery' = (StripBackticks('/* Delete Slider Group */
DELETE FROM '+($svSiteDatabase)+'.'+($svSliderGroupsTable)+' WHERE SG_GroupID = "'+($vID)+'";
/* Delete Join table entries */
DELETE FROM '+($svSG2SliderTable)+' WHERE SG_GroupID = "'+($vID)+'";
/* Delete Configuration entries */
DELETE FROM '+($svSiteDatabase)+'.'+($svSliderConfigTable)+' WHERE SG_GroupID = "'+($vID)+'" AND Slider_SliderTypeID = "'+($SliderTypeID)+'";')));

	If($svDebug == 'Y');
		('312: SQLDeleteQuery = '+($SQLDeleteQuery)+'<br>\n');
		('312: vError = '+($vError)+'<br>\n');
		('312: vOption = '+($vOption)+'<br>\n');
	/If;

	// Delete the record
	Inline($IV_Sliders, -SQL=$SQLDeleteQuery);
		// If delete successful, output 1021 "Deletion Successful" and go ahead and delete attachments,
		If((Error_Code) == '0');
			$vError = '1021';
			$vOption = $vID;
		// If delete failed, output error 1022, "Delete Failed"
		Else;
			$vError = '1022';
			$vOption = Error_CurrentError;
			LI_URLRedirect(-Page=(($svAdminControllersPath)+($svAddResponsePage)),-ExParams=('DataType='+($vDataType)),-UseError='Y',-Error=$vError,-Option=$vOption);
		/If;
	/Inline;

		
// Action is wrong, somebody is messing with us
// Dump out error 9002, "Database Error"
Else;
	$vError = '9002';
/If;


// Deal with the results of the processing

// ============= ADD  ========
// If any other error other than 1001 "Add Successful" do a redirect
If($vAction == 'Add');
	// Standard Error Table
	If($vError == '1001');
		LI_ShowError3(-ErrNum=$vError, -Option=$vOption);

// Edit New Listing Link -->
?>
<div align="left" style="margin-bottom:20px;">
	View the record for <a href="[$svAdminControllersPath][$svEditRecordPage]?Datatype=SliderGroup&ID=[Var('vNewID')]&New=Y"><b>[$vsg_name]</b></a>
</div>
<?Lassoscript
	Else;
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svAddPage)),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y');
	/If;
/If;


// ============= EDIT ========
// If vAction = Edit, Always redirect to edit page
// If record updated OK, will get a 1011 "Update Successful" message, but do NOT pass the Params, only the ID
// This forces the system to do a new lookup
If($vAction == 'Update');
	If($vError == '1011');
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svEditRecordPage)),-ExParams=('DataType='+($vDataType)+'&ID='+($vNewID)+'&SliderTypeID='+($SliderTypeIDSubmit)+'&New=Y'),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='N');
	Else;
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svEditRecordPage)),-ExParams=('DataType='+($vDataType)+'&New=Y'),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y');
	/If;
/If;

// ============= ASSIGN  ========
// If any other error other than 1011 "Add Successful" do a redirect
If($vAction == 'Assign');
	If($vError == '1011');
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svEditRecordPage)),-ExParams=('DataType='+($vDataType)+'&ID='+($vSliderGroupID)+'&New=Y'),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='N');
	Else;
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svEditRecordPage)),-ExParams=('DataType='+($vDataType)+'&New=Y'),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y');
	/If;
/If;

// ============= DELETE ========
// If Action = Delete, redirect to search page
If($vAction == 'Delete');
	LI_URLRedirect(-Page=(($svAdminControllersPath)+($svSearchPage)),-ExParams=('DataType='+($vDataType)),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='N');
/If;
?>
